package com.example.smartcity.Service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.example.smartcity.Entity.JDDongTai;
import com.example.smartcity.Entity.News;
import com.example.smartcity.Mappers.DongTaiMapper;
import com.example.smartcity.Mappers.NewsMapper;
import com.example.smartcity.Service.DongTaiService;
import com.example.smartcity.utils.MybatisUtils;
import com.google.gson.Gson;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.List;
@Service("DongTaiService")
public class DongTaiServiceImpl implements DongTaiService {
    public String findAll(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        DongTaiMapper newsMapper =sqlSession.getMapper(DongTaiMapper.class);
        List<JDDongTai> news = newsMapper.findAll();
        sqlSession.close();
        Gson gson = new Gson();
        String jsonString = gson.toJson(news);
        System.out.println(jsonString);
        JSONObject map = new JSONObject();
        map.put("total",news.size());
        map.put("rows",news);
        map.put("code",200);
        map.put("msg","查询成功");
        System.out.println(JSONObject.toJSONString(map));
//        try{
//            List<News> news2 = map.getJSONArray("rows").toJavaList(News.class);
//            for (News news3 : news2) {
//                System.out.println(news3.getTitle());
//                // 对news对象进行操作
//            }
//        }catch (Exception e){
//            System.out.println(e);
//        }

        return JSONObject.toJSONString(map);
    }
}
